package com.cskaoyan.javase.oop1._9call;

/**
 * 值传递
 * @since 09:47
 * @author wuguidong@cskaoyan.onaliyun.com
 */
public class Demo {
    public static void main(String[] args) {
        Student s1 = new Student(18);
        Student s2 = new Student(81);
        swapStudentAge(s1, s2);
        System.out.println("s1 = " + s1.age);
        System.out.println("s2 = " + s2.age);

        System.out.println("-----------------------------");
        System.out.println(s1);
        System.out.println(s2);
        swapReference(s1, s2);
        System.out.println(s1);
        System.out.println(s2);
    }

    /*
        方法得到的是引用的拷贝
        但是引用拷贝仍然指向之前的对象
        所以通过这个拷贝引用可以改变对象的状态
     */
    public static void swapStudentAge(Student s1, Student s2) {
        int temp = s1.age;
        s1.age = s2.age;
        s2.age = temp;
    }

    /*
        方法得到的是引用的拷贝
        在方法内部交换引用,是交换的拷贝引用
        对原先方法中的引用没有任何影响
     */
    public static void swapReference(Student s1, Student s2) {
        Student temp = s1;
        s1 = s2;
        s2 = temp;
    }
}

class Student {
    int age;

    public Student(int age) {
        this.age = age;
    }
}
